﻿using LD.Host;
using LdCommunication.Core.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.BC
{
    public partial class FrmIoSet : Form
    {
        private int _editIndex = -1;
        public FrmIoSet()
        {
            InitializeComponent();
            this.dgv_io.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            FrmIoSetEdit ioAdd = new FrmIoSetEdit();
            ioAdd.AddAction += Add;
            ioAdd.ShowDialog();
        }
        private void Add(TB_IO io)
        {
            this.dgv_io.Rows.Add(io.No, io.Name, (IoType)io.IoType, io.ValidateLevel, io.FixType);

            BcFactory.Instance.Config.IoList.Add(new TB_IO()
            {
                No = io.No,
                Name = io.Name,
                IoType = io.IoType,
                ValidateLevel = io.ValidateLevel,
                FixType = io.FixType,
            });
            BcFactory.Instance.Save();
        }
        private void Edit(TB_IO io)
        {
            if (_editIndex >= 0)
            {
                this.dgv_io["IO地址", _editIndex].Value = io.No;
                this.dgv_io["IO名称", _editIndex].Value = io.Name;
                this.dgv_io["IO类型", _editIndex].Value = (IoType)io.IoType;
                this.dgv_io["有效电平", _editIndex].Value = io.ValidateLevel;
                this.dgv_io["固定类型", _editIndex].Value = io.FixType;

                var model = BcFactory.Instance.Config.IoList.Where(p => p.IoType == io.IoType && p.No == io.No).FirstOrDefault();
                model.Name = io.Name;
                model.IoType = io.IoType;
                model.ValidateLevel = io.ValidateLevel;
                model.FixType = io.FixType;
                BcFactory.Instance.Save();
            }
        }
        private void btn_edit_Click(object sender, EventArgs e)
        {
            var rows = this.dgv_io.SelectedRows;
            if (rows.Count > 0)
            {
                _editIndex = rows[0].Index;

                FrmIoSetEdit ioAdd = new FrmIoSetEdit(new TB_IO()
                {
                    No = Convert.ToInt32(rows[0].Cells["IO地址"].Value),
                    Name = rows[0].Cells["IO名称"].Value.ToString(),
                    IoType = (int)(IoType)Enum.Parse(typeof(IoType), rows[0].Cells["IO类型"].Value.ToString()),
                    ValidateLevel = (LevelType)rows[0].Cells["有效电平"].Value,
                    FixType = rows[0].Cells["固定类型"].Value == null ? "" : rows[0].Cells["固定类型"].Value.ToString(),
                });
                ioAdd.AddAction += Edit;
                ioAdd.ShowDialog();
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            var rows = this.dgv_io.SelectedRows;
            if (rows.Count > 0)
            {
                IoType ioType = (IoType)Enum.Parse(typeof(IoType), rows[0].Cells["IO类型"].Value.ToString());
                var io = BcFactory.Instance.Config.IoList.Where(p => p.IoType == (int)ioType && p.No == Convert.ToInt16(rows[0].Cells["IO地址"].Value)).FirstOrDefault();
                BcFactory.Instance.Config.IoList.Remove(io);
                this.dgv_io.Rows.Remove(rows[0]);
                BcFactory.Instance.Save();
            }
        }

        private void FrmIoSet_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            //io
            if (BcFactory.Instance.Config != null)
            {
                var ioList = BcFactory.Instance.Config.IoList.OrderBy(p => p.IoType).ToList();
                foreach (var item in ioList)
                {
                    int index = this.dgv_io.Rows.Add();

                    this.dgv_io["IO地址", index].Value = item.No;
                    this.dgv_io["IO名称", index].Value = item.Name;
                    this.dgv_io["IO类型", index].Value = Enum.Parse(typeof(IoType), item.IoType + "");
                    this.dgv_io["有效电平", index].Value = item.ValidateLevel;
                    this.dgv_io["固定类型", index].Value = item.FixType;
                }
            }
        }
    }
}
